package sort.advanced_sort;

import org.junit.Test;

import java.util.Arrays;

public class SortTest {
    //快速排序测试
    @Test
    public void quickSortTest() {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            quickSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        System.out.println("未排序数组：");
        printArray(arr);

        System.out.println("升序排列：");
        quickSort(arr);
        printArray(arr);
    }
    public void quickSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        quickSort(arr, 0, arr.length - 1);
    }
    public void quickSort(int[] arr, int lo, int hi) {
        if (lo >= hi) {
            return;
        }
        int r = lo + (int) (Math.random() * (hi - lo + 1));
        swap(arr, r, lo);
        int pivotIndex = partition(arr, lo, hi);
        quickSort(arr, lo, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, hi);
    }
    public int partition(int[] arr, int lo, int hi) {
        int pivot = arr[lo];
        int mark = lo;
        for (int i = lo + 1; i <= hi; i++) {
            if (arr[i] <= pivot) {
                mark++;
                swap(arr, i, mark);
            }
        }
        arr[lo] = arr[mark];
        arr[mark] = pivot;
        return mark;
    }


    //归并排序测试
    @Test
    public void mergeSortTest() {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            mergeSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        System.out.println("未排序数组：");
        printArray(arr);

        System.out.println("升序排列：");
        mergeSort(arr);
        printArray(arr);
    }
    public static void mergeSort(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        sort(nums, 0, nums.length - 1);
    }
    public static void sort(int[] nums, int lo, int hi) {
        if (lo >= hi) {
            return;
        }
        int mid = lo + (hi - lo) / 2;
        sort(nums, lo, mid);
        sort(nums, mid + 1, hi);
        merge(nums, lo, mid, hi);
    }
    public static void merge(int[] nums, int lo, int mid, int hi) {
        int[] helper = new int[hi - lo + 1];
        int left = lo, right = mid + 1;
        for (int i = 0; i < helper.length; i++) {
            if (left > mid) {
                helper[i] = nums[right++];
            } else if (right > hi) {
                helper[i] = nums[left++];
            } else if (nums[left] <= nums[right]) {
                helper[i] = nums[left++];
            } else {
                helper[i] = nums[right++];
            }
        }
        for (int i = 0; i < helper.length; i++) {
            nums[i + lo] = helper[i];
        }
    }


    //堆排序测试
    @Test
    public void heapSortTest() {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            heapSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        System.out.println("未排序数组：");
        printArray(arr);

        System.out.println("升序排列：");
        heapSort(arr);
        printArray(arr);
    }
    public void downAdjust(int[] nums, int parentIndex, int size) {
        int childIndex = parentIndex * 2 + 1;
        while (childIndex < size) {
            if (childIndex + 1 < size && nums[childIndex + 1] > nums[childIndex]) {
                childIndex++;
            }
            if (nums[parentIndex] >= nums[childIndex]) {
                break;
            }
            swap(nums, parentIndex, childIndex);
            parentIndex = childIndex;
            childIndex = parentIndex * 2 + 1;
        }
    }
    public void heapSort(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        //1.无序数组构建成堆
        for (int i = (nums.length - 2) / 2; i >= 0; i--) {
            downAdjust(nums, i, nums.length);
        }
        //2.循环删除堆顶
        for (int i = nums.length - 1; i > 0; i--) {
            swap(nums, i, 0);
            downAdjust(nums, 0, i);
        }
    }







    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }
}
